/** Copyright 2014, Ross Mahony. Some rights reserved. See COPYING. */
package models

import play.api.db.slick.Config.driver.simple._
import scala.slick.lifted.Tag

case class Product(id: Option[Long], name: String, description: String, meterTypeId: Long)

/** Product entity. */
class ProductTbl(tag: Tag) extends Table[Product](tag, "PROD") {

  def id = column[Long]("ID", O.PrimaryKey, O.AutoInc)
  def name = column[String]("NAME", O.NotNull)
  def description = column[String]("DESCR", O.Nullable)
  def meterTypeId = column[Long]("MTR_TYP_ID", O.NotNull)
  def * = (id.?, name, description, meterTypeId) <> (Product.tupled, Product.unapply _)
}

/** Product DAO */
object ProductDAO {
  val products = TableQuery[ProductTbl]
  
 /**
  * Retrieve a [[models.Product]] by the id
  *
  * @param id Product ID
  */
  def findById(id: Long)(implicit s: Session): Option[Product] =
  products.where(_.id === id).firstOption


 /** Count all products */
  def count(implicit s: Session): Int =
  Query(products.length).first

 /**
  * Insert a new [[models.Product]]
  *
  * @param product
  */
  def insert(product: Product)(implicit s: Session) {
    products.insert(product)
  }

 /**
  * Insert a new [[models.Product]] by ignoring auto inc column
  *
  * @param product
  */
  def forceInsert(product: Product)(implicit s: Session) {
    products.forceInsert(product)
  }

 /** Delete all products */
  def deleteAll(implicit s: Session) {
    products.delete
  }

}
